﻿using System;
using System.Threading.Tasks;
using Microsoft.Azure.ActiveDirectory.GraphClient;
using Microsoft.Azure.ActiveDirectory.GraphClient.Extensions;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Configuration;
using System.Collections.Generic;

namespace AzureAD_Schema_Manager
{
    public class common
    {
        public common()
        {
            TenantName = ConfigurationManager.AppSettings["TenantName"];
            ApplicationName = ConfigurationManager.AppSettings["ApplicationName"];
            ClientId = ConfigurationManager.AppSettings["ApplicationClientId"];
            ClientSecret = ConfigurationManager.AppSettings["ApplicationClientSecret"];
        }
        // properties
        public string TenantName;
        public string ApplicationName;
        public string ClientId;
        public string ClientSecret;
        // create AzureAD client
        public ActiveDirectoryClient GetActiveDirectoryClientAsApplication(
            string tenantName,
            string clientId,
            string clientSecret)
        {
            TenantName = tenantName;
            ClientId = clientId;
            ClientSecret = clientSecret;
            return GetActiveDirectoryClientAsApplication();
        }
        public ActiveDirectoryClient GetActiveDirectoryClientAsApplication()
        {
            Uri servicePointUri = new Uri(ConfigurationManager.AppSettings["ResourceEndPoint"]);

            Uri serviceRoot = new Uri(servicePointUri, TenantName);

            ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot, async () => await AcquireTokenAsyncForApplication());

            return activeDirectoryClient;

        }

        public IEnumerable<IExtensionProperty> GetExtensions(ActiveDirectoryClient adclient)
        {
            IEnumerable<IExtensionProperty> ret = null;
            Task task = Task.Factory.StartNew(async () =>
            {
                ret = await GetEx(adclient);
            }).Unwrap();
            task.Wait();
            return ret;
        }

        private async Task<IEnumerable<IExtensionProperty>> GetEx(ActiveDirectoryClient adclient)
        {
            return adclient.GetAvailableExtensionPropertiesAsync(false).Result;
        }

        private async Task<string> AcquireTokenAsyncForApplication()
        {
                return GetTokenForApplication();
        }

        private string GetTokenForApplication()
        {
            AuthenticationContext authenticationContext = new AuthenticationContext(
                ConfigurationManager.AppSettings["AuthEndPoint"] + "/" + TenantName,
                false);
            // Config for OAuth client credentials 
            ClientCredential clientCred = new ClientCredential(ClientId, ClientSecret);
            AuthenticationResult authenticationResult = authenticationContext.AcquireToken(
                ConfigurationManager.AppSettings["ResourceEndPoint"],
                clientCred);
            string token = authenticationResult.AccessToken;
            return token;
        }

    }
}
